package com.example.arithmeticleetcode.learnArithmetic2.sort;

import java.text.DecimalFormat;

/**
 * @program: arithmetic-leetcode
 * @description:
 * @author: FangZhen
 * @create: 2020-11-05 11:55
 **/
public abstract class Sort<E extends Comparable<E>> implements Comparable<Sort<E>>{

    protected E[] array;
    private int cmpCount;
    private int swapCount;
    private long time;
    private DecimalFormat fmt = new DecimalFormat("#.00");

    public void sort(E[] array) {
        if (array == null || array.length < 2) {
            return;
        }
        this.array = array;

        long beginTime = System.currentTimeMillis();
        sort();
        time = System.currentTimeMillis() - beginTime;

    }

    protected abstract void sort();

    @Override
    public int compareTo(Sort<E> o) {
        int result = (int)(this.time - o.time);
        if (result != 0) return result;
        result = cmpCount - o.cmpCount;
        if (result != 0) return result;
        return swapCount - o.swapCount;
    }

    /**
     *
     * @param i1
     * @param i2
     * @return 0  array[i1] = array[i2]   < 0  array[i1] < array[i2]  >0  array[i1] > array[i2]
     */
    protected int cmp(int i1, int i2) {
        cmpCount++;
        return array[i1].compareTo(array[i2]);
    }

    protected int cmp(E v1, E v2) {
        cmpCount++;
        return v1.compareTo(v2) ;
    }

    protected void swap(int i1, int i2) {
        swapCount++;
        E temp = array[i1];
        array[i1] = array[i2];
        array[i2] = temp;
    }

    @Override
    public String toString() {
        String timeStr = "耗时：" + (time / 1000.0) + "s(" + time + "ms)";
        String compareCountStr = "比较：" + numberString(cmpCount);
        String swapCountStr = "交换：" + numberString(swapCount);
        String stableStr = "稳定性： " + isStable();
        return "【" + getClass().getSimpleName() + "】\n"
                + stableStr + " \t"
                + timeStr + " \t"
                + compareCountStr + "\t "
                + swapCountStr + "\n"
                + "------------------------------------------------------------------";
    }

    private boolean isStable() {

        if (this instanceof RedixSort) return true;
        if (this instanceof CountingSort) return true;
        if (this instanceof SelectionSort) return false;

        Student[] students = new Student[20];
        for (int i = 0, len = students.length; i < len; i++) {
            students[i] = new Student(i * 10, 10);
        }
        sort((E[])students);
        for (int i = 1, len = students.length; i < len; i++) {
            int score = students[i].score;
            int preScore = students[i - 1].score;
            if (score - preScore != 10) {
                return false;
            }
        }
        return true;
    }

    private String numberString(int number) {
        if (number < 10000) return "" + number;

        if (number < 100000000) return fmt.format(number / 10000.0) + "万";
        return fmt.format(number / 100000000.0) + "亿";
    }

}
